package org.jeecg.modules.xindian.reconciliation.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import kotlin.reflect.jvm.internal.impl.util.Check;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.xindian.reconciliation.controller.req.AddNoteReqInfo;
import org.jeecg.modules.xindian.reconciliation.controller.req.ReportReq;
import org.jeecg.modules.xindian.reconciliation.controller.req.UnCheckQuery;
import org.jeecg.modules.xindian.reconciliation.controller.rsp.UnCheckPolicyInfo;
import org.jeecg.modules.xindian.reconciliation.dto.rsp.ChannelReportInfo;
import org.jeecg.modules.xindian.reconciliation.entity.PolicySettlementTypeStatistics;
import org.jeecg.modules.xindian.reconciliation.entity.PolicyUnSettlementTypeStatistics;
import org.jeecg.modules.xindian.reconciliation.service.IPolicySettlementTypeStatisticsService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.xindian.reconciliation.service.IPolicyUnSettlementTypeStatisticsService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 结算类型汇总保单关联
 * @Author: jeecg-boot
 * @Date:   2024-06-24
 * @Version: V1.0
 */
@Api(tags="结算类型汇总保单关联")
@RestController
@RequestMapping("/reconciliation/policySettlementTypeStatistics")
@Slf4j
public class UnCheckTypeStatisticsController extends JeecgController<PolicySettlementTypeStatistics, IPolicySettlementTypeStatisticsService> {
	@Autowired
	private IPolicySettlementTypeStatisticsService policySettlementTypeStatisticsService;
	@Autowired
	private IPolicyUnSettlementTypeStatisticsService policyUnSettlementTypeStatisticsService;
	
	/**
	 * 分页列表查询
	 *
	 * @param policySettlementTypeStatistics
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "结算类型汇总保单关联-分页列表查询")
	@ApiOperation(value="结算类型汇总保单关联-分页列表查询", notes="结算类型汇总保单关联-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<UnCheckPolicyInfo>> queryPageList(PolicySettlementTypeStatistics policySettlementTypeStatistics,
														  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														  UnCheckQuery unCheckQuery) {
		//查询冲突的订单列表
		Page<PolicySettlementTypeStatistics> page = new Page<PolicySettlementTypeStatistics>(pageNo, pageSize);
		return Result.OK(policySettlementTypeStatisticsService.queryUnCheckInfo(page,unCheckQuery));
	}



	/**
	 * 渠道报表
	 *
	 * @return
	 */
	//@AutoLog(value = "杭州银行资金流水-分页列表查询")
	@ApiOperation(value = "渠道报表Excel导出", notes = "渠道报表Excel导出")
	@GetMapping(value = "/checkExcel")
	public ModelAndView channelReportExcel(ReportReq req, HttpServletRequest request,UnCheckQuery unCheckQuery) {
		List<UnCheckPolicyInfo>  list = policySettlementTypeStatisticsService.queryUnCheckInfoList(unCheckQuery);
		return this.exportXls(request, list, UnCheckPolicyInfo.class, "撞单数据");
	}


	/**
	 * 导出excel
	 *
	 * @param request
	 */
	protected <T> ModelAndView exportXls(HttpServletRequest request, List<T> exportList, Class<T> clazz, String title) {
		// Step.1 组装查询条件
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		//此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.FILE_NAME, title);
		mv.addObject(NormalExcelConstants.CLASS, clazz);
		//update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
		ExportParams exportParams = new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
		exportParams.setType(ExcelType.HSSF);
		mv.addObject(NormalExcelConstants.PARAMS, exportParams);
		mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		return mv;
	}


	/**
	 * 处理选择的保单数据
	 * @param
	 * @return
	 */
	@AutoLog(value = "撞单选择")
	@ApiOperation(value="撞单选择", notes="撞单选择")
	@PostMapping(value = "/selectCheck")
	public Result<?> selectCheck(@RequestBody PolicySettlementTypeStatistics statistics) {
		Assert.notNull(statistics.getId(),"选择的数据不能未空");
		policySettlementTypeStatisticsService.selectCheck(statistics.getId());
		return Result.OK("操作成功");
	}


	/**
	 * 添加撞单记录信息
	 * @param
	 * @return
	 */
	@AutoLog(value = "添加撞单记录信息")
	@ApiOperation(value="添加撞单记录信息", notes="添加撞单记录信息")
	@PostMapping(value = "/addNote")
	public Result<?> addNote(@RequestBody AddNoteReqInfo addNoteReqInfo) {
		Assert.notNull(addNoteReqInfo.getPolicyNo(),"保单号不能为空");
		policySettlementTypeStatisticsService.lambdaUpdate()
				.eq(PolicySettlementTypeStatistics::getPolicyNo,addNoteReqInfo.getPolicyNo())
				.set(PolicySettlementTypeStatistics::getCollisionResultsNote,addNoteReqInfo.getCollisionResultsNote()).update();
		return Result.OK("操作成功");
	}

	/**
	 * 处理选择的保单数据
	 * @param
	 * @return
	 */
	@AutoLog(value = "单月汇总处理")
	@ApiOperation(value="单月汇总处理", notes="单月汇总处理")
	@PostMapping(value = "/processPolicySettlement")
	public Result<?> processPolicySettlement(@RequestBody PolicySettlementTypeStatistics statistics) {
		policySettlementTypeStatisticsService.processPolicySettlement(statistics.getDate());
		return Result.OK("操作成功");
	}
}
